package cn.com.huzo.webapp.controller.master;

import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.master.MaterialGroupEntity;
import cn.com.huzo.model.entities.master.MaterialGroupMemberUserEntity;
import cn.com.huzo.service.master.MaterialGroupManager;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cn.com.huzo.service.master.MaterialGroupMemberUserManager;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 材料组管理控制器
 *
 * @author 吴旭
 */
@Controller
public class MaterialGroupController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "MaterialGroupManager")
    private MaterialGroupManager _manager;

    @Resource(name = "MaterialGroupMemberUserManager")
    private MaterialGroupMemberUserManager _manager_lines;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到材料组一览画面
     * <p>
     * @return
     */
    @RequestMapping("master/material_group/list.html")
    public ModelAndView redirectListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("master.material_group.list.def");
        return view;
    }

    /**
     * 跳转到材料组编辑画面
     * <p>
     * @param action 操作类别 1：增加 2：编辑
     * @param groupId 材料组标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("master/material_group/{action}/{groupId}/edit.html")
    public ModelAndView redirectEditWnd(@PathVariable(value = "action") int action,
            @PathVariable(value = "groupId") Long groupId) throws Exception {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量
        view.addObject("action", action);
        view.addObject("groupId", groupId);
        view.setViewName("master.material_group.edit.def");
        return view;
    }

    /**
     * 弹出材料组选择画面
     * <p>
     * @return
     */
    @RequestMapping("master/material_group/choice.html")
    public ModelAndView redirectMaterialGroupPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.master.material_group.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="材料组数据处理部分">
    /**
     * 根据标识取得材料组数据。
     *
     * @param criterion 材料组标识
     * @return 材料组数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/material_group/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.fetchByHeaderKey(MapUtils.getLong(criterion, "GroupId"),
                MapUtils.getInteger(criterion, "Limit"),
                MapUtils.getInteger(criterion, "PageIndex"));
    }

    /**
     * 根据条件取得系统成员分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 系统成员分页数据
     * @throws Exception
     */
    @RequestMapping(value = "master/material_group/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager.fetchForPage(criterion);
    }

    /**
     * 材料组数据增加或者修改
     *
     * @param instance 要处理的数据
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/material_group/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody final MaterialGroupEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getGroupId() > 0) {
            rowsAffected = (long) _manager.update(instance);
        } else {
            rowsAffected = _manager.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 删除。
     *
     * @param criterion 要删除的数据
     * @return 返回删除后的数据
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/material_group/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager.delete(criterion);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="材料组所属用户成员数据处理部分">
    /**
     * 根据条件取得材料组成员分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 材料组成员分页数据
     * @throws Exception
     */
    @RequestMapping(value = "master/material_group/lines/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchLinesForPage(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.fetchForPage(criterion);
    }

    /**
     * 批量增加材料组所属成员用户。
     *
     * @param criterion 要处理的数据列表
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/material_group/lines/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchInsert(@RequestBody final Map<String, Object> criterion) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        List<MaterialGroupMemberUserEntity> _itemList = mapper.convertValue(MapUtils.getObject(criterion, "ItemList"), new TypeReference<List<MaterialGroupMemberUserEntity>>() {
        });

        return _manager_lines.batchInsert(_itemList,
                MapUtils.getInteger(criterion, "Limit"),
                MapUtils.getInteger(criterion, "PageIndex"));
    }

    /**
     * 删除材料组所属成员用户。
     *
     * @param criterion 删除条件
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/material_group/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.deleteLines(
                MapUtils.getLong(criterion, "GroupId"),
                MapUtils.getString(criterion, "GroupName"),
                MapUtils.getLong(criterion, "EmployeeId"),
                MapUtils.getString(criterion, "MemberName"),
                MapUtils.getLong(criterion, "DeletedBy"),
                MapUtils.getInteger(criterion, "Limit"),
                MapUtils.getInteger(criterion, "PageIndex")
        );
    }
    // </editor-fold>
}
